package db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import db.Controller;
import db.dto.WordsDTO;

public class WordsDAO {
	public List<WordsDTO> getWord(int dictID) {
		List<WordsDTO> listWord = new ArrayList<WordsDTO>();
		String query = "select * from m_words where m_dictID = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, dictID);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				listWord.add(new WordsDTO(rs.getInt("m_id"), 
										rs.getInt("m_dictID"), 
										rs.getString("m_word"), 
										rs.getString("m_meaning"), 
										rs.getInt("m_status")));
			}
		} catch (SQLException e) {
			return null;
		}
		return listWord;
	}
	public WordsDTO getAWord(int id) {
		String query = "select * from m_words where m_id = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				return (new WordsDTO(rs.getInt("m_id"), 
										rs.getInt("m_dictID"), 
										rs.getString("m_word"), 
										rs.getString("m_meaning"), 
										rs.getInt("m_status")));
			}
		} catch (SQLException e) {
			return null;
		}
		return null;
	}
	public int getWordReviewerCount(int dictID) {
		String query = "select count(*) as word_count from m_words where m_dictID = ? and m_status = 0 group by m_id";
		Connection con = Controller.getConnection();
		int result = 0;
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, dictID);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				result = rs.getInt("word_count");
			}
		} catch (SQLException e) {
			return 0;
		}
		return result;
	}
	
	public int getWordApproverCount(int dictID) {
		String query = "select count(*) as word_count from m_words where m_dictID = ? and m_status = 0 group by m_id";
		Connection con = Controller.getConnection();
		int result = 0;
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, dictID);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				result = rs.getInt("word_count");
			}
		} catch (SQLException e) {
			return 0;
		}
		return result;
	}
	
	public boolean deleteWord(int id) {
		String query = "delete from m_words where m_id = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	public boolean addWord(int dictID, String word, String meaning, int status) {
		String query = "insert into m_words(m_dictID, m_word, m_meaning, m_status) values (?, ?, ?, ?)";
		Connection con = Controller.getConnection();
		PreparedStatement stmt;
		try {
			stmt = con.prepareStatement(query);
			stmt.setInt(1, dictID);
			stmt.setString(2, word);
			stmt.setString(3, meaning);
			stmt.setInt(4, status);
			stmt.executeUpdate();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	public boolean setStatusWord(int id) {
		String query = "update m_words set m_status = 1 where m_id = ?";
		Connection con = Controller.getConnection();
		PreparedStatement stmt;
		try {
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			stmt.execute();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
}
